﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace PivotBeer
{
    class Program
    {
        static void Main(string[] args)
        {

            TextReader tr = new StreamReader("..\\..\\beers.txt");
            string s = tr.ReadLine();
            PivotCreator<Beer> pc = new PivotCreator<Beer>("beer.cxml", "Beer", "beer.xml");

            try
            {
                while (true)
                {
                    if (s == null)
                    {
                        s = tr.ReadLine();
                        continue;
                    }
                    
                    string html = BeerSpider.GetHtml(s);
                    Console.WriteLine(html);
                    Beer b = new Beer(s, html);
                    WebClient wc = new WebClient();
                    try
                    {
                        
                        wc.DownloadFile(new Uri("http://beeradvocate.com/im/beers/" + b.ID + ".jpg"), "images/" + b.ID + ".jpg");
                        pc.AddItem(b, 0, b.ID, b.Link, b.BeerName, "");
                    }
                    catch (WebException we)
                    {
                        try
                        {
                            wc.DownloadFile(new Uri("http://beeradvocate.com/im/beers/no_beer_pic.jpg"), "images/" + b.ID + ".jpg");
                            pc.AddItem(b, 0, b.ID, b.Link, b.BeerName, "");
                        }
                        catch (WebException)
                        {
                            continue;
                        }
                    }
                    if (tr.Peek() == -1)
                    {
                        break;
                    }
                    s = tr.ReadLine();
                    System.Threading.Thread.Sleep(10000);
                }
            }
            catch (IOException)
            {


            }
            pc.Close();
            tr.Close();
            
        }
    }
}
